//
//  EFCouponViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/7.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFCouponViewCell.h"
#import "EFCouponViewItem.h"

@interface EFCouponViewCell ()
@property(strong,nonatomic) UIImageView* leftBgIV;
@property(strong,nonatomic) UILabel* priceLabel;
@property(strong,nonatomic) UILabel* priceTipsLabel;

@property(strong,nonatomic) UILabel* descLabel;
@property(strong,nonatomic) UILabel* dateLabel;

@property(strong,nonatomic) UIButton* useBtn;

@property(strong,nonatomic) UIButton* selectButton;

@property(strong,nonatomic) NSString* id;

@end

@implementation EFCouponViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)setObject:(EFCouponViewItem *)item{
    self.id = item.id;
    NSMutableAttributedString * strPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",item.fee]];
    
    NSRange range = NSMakeRange([[strPrice string] rangeOfString:@"￥"].location, [[strPrice string] rangeOfString:@"￥"].length);
    [strPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:range];
    
//    if(item.isCoupon){
//        strPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@代金券",item.price]];
//        
//        NSRange range2 = NSMakeRange([strPrice length] - 3, 3);
//        [strPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:range2];
//    }
    
    [self.priceLabel setAttributedText:strPrice];
    [self.priceLabel sizeToFit];
    [self.priceTipsLabel setText:item.rule];
    [self.priceTipsLabel sizeToFit];
    [self.descLabel setText:item.title];
    [self.descLabel sizeToFit];
    
    NSString* date = [NSString stringWithFormat:@"%@-%@",item.started_at,item.closed_at];
    [self.dateLabel setText:date];
    [self.dateLabel sizeToFit];
    
    if(item.status == 0){
        [self.leftBgIV setImage:IMG(@"fulldown_bg_image_default")];
        self.useBtn.hidden = false;
    }else if(item.status == 2){
        [self.leftBgIV setImage:IMG(@"vouchers_bg_image_default")];
        self.useBtn.hidden = true;
    }
    
    if(item.fromOrder){
        self.selectButton.hidden = false;
        self.useBtn.hidden = true;
        [self.selectButton setSelected:item.selected];
        
    }else{
        self.selectButton.hidden = true;
        self.useBtn.hidden = false;
    }
}

-(void)initView{
    [self.leftBgIV addSubview:self.priceLabel];
    [self.leftBgIV addSubview:self.priceTipsLabel];
    [self.contentView addSubview:self.leftBgIV];
    [self.contentView addSubview:self.descLabel];
    [self.contentView addSubview:self.dateLabel];
   // [self.contentView addSubview:self.useBtn];
    [self.contentView addSubview:self.selectButton];
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.leftBgIV.width = 120;
    self.leftBgIV.height = 90;
    self.leftBgIV.left = 20;
    self.leftBgIV.top = 20;
    
    self.priceLabel.left = 21;
    self.priceLabel.top = 19;
    self.priceLabel.height = 32;
    
    self.priceTipsLabel.left = 21;
    self.priceTipsLabel.top = self.priceLabel.bottom + 1;
    self.priceTipsLabel.height = 15;
    
    
    self.descLabel.left = self.leftBgIV.right +  19;
    self.descLabel.top = 20 + 14;
    
    
    self.dateLabel.left =  self.leftBgIV.right + 19;
    self.dateLabel.top = self.descLabel.bottom + 4;
    
    
    self.useBtn.left =  self.leftBgIV.right + 19;
    self.useBtn.top = self.dateLabel.bottom + 7;
    self.useBtn.width = 76;
    self.useBtn.height = 25;
    
    self.selectButton.left = kScreenWidth - self.selectButton.width - 40;
    self.selectButton.top = (self.height -  self.selectButton.height)/2;
}

-(UIImageView*)leftBgIV{
    if(_leftBgIV == NULL){
        _leftBgIV = [[UIImageView alloc] init];
    }
    return _leftBgIV;
}

-(UILabel*)priceLabel{
    if(_priceLabel == NULL){
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:20];
        _priceLabel.textColor  = [UIColor whiteColor];
    }
    return _priceLabel;
}

-(UILabel*)priceTipsLabel{
    if(_priceTipsLabel == NULL){
        _priceTipsLabel = [[UILabel alloc] init];
        _priceTipsLabel.font = [UIFont systemFontOfSize:13];
        _priceTipsLabel.textColor  = [UIColor whiteColor];
    }
    return _priceTipsLabel;
}

-(UILabel*)descLabel{
    if(_descLabel == NULL){
        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
        _descLabel.font = [UIFont systemFontOfSize:13];
        _descLabel.textColor  = kBlack666;
        _descLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _descLabel;
}


-(UILabel*)dateLabel{
    if(_dateLabel == NULL){
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
        _dateLabel.font = [UIFont systemFontOfSize:10];
        _dateLabel.textColor  = kBlack999;
        _dateLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _dateLabel;
}

-(UIButton*)useBtn{
    if(_useBtn == NULL){
        _useBtn = [[UIButton alloc] init];
        [_useBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_useBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _useBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _useBtn.titleLabel.textColor = RGBCOLOR(0x69BA45);
        [_useBtn setTitle:@"立即使用" forState:UIControlStateNormal];
    }
    return _useBtn;
}


- (UIButton *)selectButton{
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22.f, 22.f)];
        [_selectButton setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_selectButton setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_selectButton setImage:IMG(@"cartlist_failure_icon") forState:UIControlStateDisabled];
        [_selectButton addTarget:self action:@selector(onClickSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

-(void)onClickSelectBtn{
    self.selectButton.selected = !self.selectButton.selected;
    if(self.selectBlock){
        self.selectBlock(self.id);
    }
}





@end
